import React, { Fragment, useContext, useState } from 'react'
import { message, Spin } from 'antd';
import { addExamination, deleteExa, stuSearchExa, teaSearchExa, updateExa } from '@/api/api';
import { Context } from '@/App';
import { updateSearchInfor } from '@/tool/updateSearchInfor';
import useExamination from '@/hooks/useExamination';
import useAdditionalInfor from '@/hooks/useAdditionalInfor';
import './examination.less'

import UpdateExamination from '@/components/ExaminationUI/UpdateExamination';
import ExaminationSearch from '@/components/ExaminationUI/ExaminationSearch'
import ExaminationList from '@/components/ExaminationUI/ExaminationList';
import AddExamination from '@/components/ExaminationUI/AddExamination';

export default function Examination() {
  const [addOpen, setAddOpen] = useState(false)
  const [updateOpen, setUpdateOpen] = useState(false)
  const [loading, setLoading] = useState(false)

  const [updateData, setUpdateData] = useState()
  const [searchInfor, setSearchInfor] = useState(null)

  const { role } = useContext(Context)

  const { data: examination, setData: setExamination, getExamination, ProcessTime } = useExamination(role, setLoading)
  const { classes, curriculum, classroom } = useAdditionalInfor(role)

  // 重置
  const Reset = () => {
    setSearchInfor(null)
    getExamination(role);
  }

  // 学生点击搜索
  const studnetSearch = (data) => {
    console.log("学生收索");
    const values = updateSearchInfor(data)
    setSearchInfor(values)
    stuSearchExa({values, role }).then(
      res => {
        if (res.status === 200) {
          setExamination(ProcessTime(res.data))
        }
        if (res.status === 201) {
          message.error("没有相应的考试信息")
          setExamination(null)
        }
      },
    )
  };

  const teacherSearch = (values) => {
    setSearchInfor(values)
    teaSearchExa({ role, ...values }).then(
      res => {
        if (res.status === 200) {
          setExamination(ProcessTime(res.data))
        }
        if (res.status === 201) {
          message.error("没有符合条件的考试！！！")
        }
      }
    )
  }

  const searchExamination = role?.fd_type === "student" ? studnetSearch : role?.fd_type === "teacher" ? teacherSearch : undefined

  // 添加提交
  const addSubmit = (values) => {
    console.log('添加的考试信息', values);
    addExamination(values).then(
      res => {
        if(res.status === 200){
          message.success(res.data)
          if (searchInfor === null) {
            getExamination(role)
          } else {
            teacherSearch(searchInfor)
          }
        }
        if(res.status === 201){
          message.error("添加失败")
        }
      }
    )
    setAddOpen(false);
  };

  // 修改提交
  const updateSubmit = (values) => {
    console.log('修改的考试信息', values);
    updateExa(values).then(
      res => {
        if (res.status === 200) {
          message.success(res.data)
          if (searchInfor === null) {
            getExamination(role)
          } else {
            teacherSearch(searchInfor)
          }
        }
        if (res.status === 201) {
          message.error("修改失败")
        }
      }
    )
    setUpdateOpen(false);
  };

  // 修改
  const updateExamination = (data) => {
    setUpdateOpen(true);
    setUpdateData(data)
  }
  // 删除
  const deleExamination = (value) => {
    console.log('删除考试信息', value)
    const {fd_id,fd_claNum,fd_classroomNum,fd_curriculumNum} = value
    const data={fd_id,fd_claNum,fd_classroomNum,fd_curriculumNum}
    deleteExa(data).then(
      res =>{
        if(res.status === 200){
          message.success(res.data);
          if (searchInfor === null) {
            getExamination(role)
          } else {
            teacherSearch(searchInfor)
          }
        }
        if(res.status === 201) {
          message.error("添加失败")
        }
      }
    )
  }

  return (
    <Fragment>
      <div className='examination-box'>
        {
          loading ? <Spin size='large' className='loading' /> :
            <>
              <div className="examination-select">
                <ExaminationSearch
                  curriculum={curriculum}
                  classes={classes}
                  Reset={Reset}
                  setOpen={setAddOpen}
                  onFinish={searchExamination}
                  role={role}
                />
              </div>
              <div className='examination-table'>
                <ExaminationList
                  role={role}
                  examination={examination}
                  updateExamination={updateExamination}
                  deleExamination={deleExamination}
                />
              </div>
            </>
        }
        <AddExamination
          curriculum={curriculum}
          classes={classes}
          classrooms={classroom}
          open={addOpen}
          onCreate={addSubmit}
          onCancel={() => {
            setAddOpen(false);
          }}
        />
        <UpdateExamination
          updateData={updateData}
          curriculum={curriculum}
          classes={classes}
          classrooms={classroom}
          open={updateOpen}
          onCreate={updateSubmit}
          onCancel={() => {
            setUpdateOpen(false);
          }}
        />
      </div>
    </Fragment>
  )
}
